package com.spsy.pay.test;


import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@SpringBootTest
public class test1 {

    private static int n1 = 0;

    @Test
    public void test2() throws InterruptedException {


        //创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(10);

        //使用原子整数保证线程池安全
        AtomicInteger n = new AtomicInteger(0);


        for (int i = 0; i < 10000; i++) {
            executor.submit(() -> {
                n.incrementAndGet();//原子自增操作
//                n1 = n1 + 1;
            });
        }

        //关闭线程池并等待所有任务完成
        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.MINUTES);

        System.out.println("最终结果:" + n.get());
//        System.out.println("最终结果:" + n1);

    }


}
